package com.jty.lib_base.room

import android.content.Context
import androidx.room.ColumnInfo
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.czl.lib_base.config.AppConstants
import com.jty.lib_base.base.AppContext
import com.jty.lib_base.room.bean.*
import com.jty.lib_base.room.dao.*
import com.jty.lib_base.utils.SpHelper

/*@Database(entities = arrayOf(BookNote::class,
    GridPicture::class,
    TeachAction::class
    ), version = 1)*/
//数组可以用[]或者arrayOf
@Database(entities = [BookNote::class,
    GridPicture::class,
    TeachAction::class,
    NoteItem::class,
    ResearchItem::class,
    GridNoSubmit::class,
    CoordinateKill::class
], exportSchema = false, version = 3)
//@TypeConverters(value = arrayOf(DateConverters::class)) //类型转换
abstract class AppDatabase : RoomDatabase() {
    abstract fun bookNoteDao(): BookNoteDao
    abstract fun gridPictureDao(): GridPictureDao
    abstract fun TeachActionDao(): TeachActionDao
    abstract fun NoteItemDao() : NoteItemDao
    abstract fun ResearchItemDao() : ResearchItemDao
    abstract fun gridNoSubmitDao() : GridNoSubmitDao
    abstract fun coordinateKillDao() : CoordinateKillDao


    /**
     * 单例
     */
    companion object {
        @Volatile
        private var instance: AppDatabase? = null

        fun getInstance(): AppDatabase {
            return instance ?: synchronized(this) {
                instance ?: buildDatabase(AppContext).also { instance = it }
            }
        }

        fun resetInstance() {
            instance = null
        }

        private fun buildDatabase(context: Context): AppDatabase {
            return Room.databaseBuilder(context,
                AppDatabase::class.java,
                "${SpHelper.decodeInt(AppConstants.SpKey.USER_ID)}_${AppConstants.Db.DATABASE_NAME}.db")
                .allowMainThreadQueries()
                .addMigrations(MIGRATION_1_2, MIGRATION_2_3)
                .build()
        }

        val MIGRATION_1_2: Migration = object: Migration(1, 2){
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("CREATE TABLE IF NOT EXISTS grid_no_submit (" +
                        "id INTEGER PRIMARY KEY NOT NULL," +
                        "pageserial TEXT NOT NULL," +
                        "pageNumber INTEGER NOT NULL," +
                        "pictureName TEXT," +
                        "pictureUrl TEXT NOT NULL," +
                        "width INTEGER NOT NULL," +
                        "height INTEGER NOT NULL," +
                        "sort INTEGER NOT NULL)");
            }
        }
        val MIGRATION_2_3: Migration = object: Migration(2, 3){
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("CREATE TABLE IF NOT EXISTS coordinate_kill (" +
                        "id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                        "strCoorDownToUp TEXT NOT NULL)");
            }
        }
    }
}